package fanfou4j;

import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.james.mime4j.util.CharsetUtil;

import fanfou4j.org.json.JSONArray;
import fanfou4j.org.json.JSONObject;
import fanfou4j.type.Message;
import fanfou4j.type.Status;
import fanfou4j.type.User;
import fanfou4j.util.UrlStr;

public class Fanfou extends FanfouSupport {

	public Fanfou(String username, String password) {
		setCredentials(username, password);
	}

	public List<Status> getStatusList(NameValuePair... nameValuePairs)
			throws Exception {
		String str = doHttpGet(UrlStr.STATUSES_FRIENDS_TIMELINE, nameValuePairs);
		return Status.getStatusList(new JSONArray(str));
	}

	public String updateStatus(String str) throws Exception {
		return doHttpPost(UrlStr.STATUSES_POST_UPDATE, new BasicNameValuePair(
				"status", str));
	}

	public String deleteStatus(String idStr) throws Exception {
		return doHttpPost(UrlStr.STATUSES_POST_DESTROY, new BasicNameValuePair(
				"id", idStr));
	}

	public String uploadStatus(ContentBody photo, String description)
			throws Exception {
		String urlStr = BASEURL + UrlStr.STATUSES_POST_PHOTOS;
		HttpPost httpPost = new HttpPost(urlStr);
		MultipartEntity reqEntity = new MultipartEntity();
		reqEntity.addPart("photo", photo);
		if (description != null && description.length() > 0)
			reqEntity
					.addPart(
							"status",
							new StringBody(description, CharsetUtil
									.getCharset("UTF-8")));
		httpPost.setEntity(reqEntity);
		HttpResponse response = httpClient.execute(httpPost);
		HttpEntity resEntity = response.getEntity();
		return EntityUtils.toString(resEntity);
	}

	public List<Status> search(String q, String maxId) throws Exception {
		String str = doHttpGet(UrlStr.STATUSES_SEARCH, new BasicNameValuePair(
				"q", q), new BasicNameValuePair("max_id", maxId));
		return Status.getStatusList(new JSONArray(str));
	}

	public List<Status> search(String q) throws Exception {
		return search(q, null);
	}

	public List<User> getFriends(String userId) throws Exception {
		String str = doHttpGet(UrlStr.USERS_FRIENDS, new BasicNameValuePair(
				"id", userId));
		return User.getUserList(new JSONArray(str));
	}

	public List<User> getFollows(String userId) throws Exception {
		String str = doHttpGet(UrlStr.USERS_FOLLOWERS, new BasicNameValuePair(
				"id", userId));
		return User.getUserList(new JSONArray(str));
	}

	public User getUser(String userId) throws Exception {
		String str = doHttpGet(UrlStr.USERS_SHOW, new BasicNameValuePair("id",
				userId));
		return new User(new JSONObject(str));
	}

	public List<Message> getReceivedMessage() throws Exception {
		String str = doHttpGet(UrlStr.MESSAGES_DIRECT);
		return Message.getMessageList(new JSONArray(str));
	}

	public List<Message> getSendMessage() throws Exception {
		String str = doHttpGet(UrlStr.MESSAGES_DIRECT_SENT);
		return Message.getMessageList(new JSONArray(str));
	}

	public String sendMessage(String userId, String text, String replyId)
			throws Exception {
		return doHttpPost(UrlStr.MESSAGES_NEW, new BasicNameValuePair("user",
				userId), new BasicNameValuePair("text", text),
				new BasicNameValuePair("text", text), new BasicNameValuePair(
						"in_reply_to_id", replyId));
	}

	public String sendMessage(String userId, String text) throws Exception {
		return sendMessage(userId, text, null);
	}

	public String deleteMessage(String messageId) throws Exception {
		return doHttpPost(UrlStr.MESSAGES_DESTROY, new BasicNameValuePair("id",
				messageId));
	}

	public List<Status> getFavorites(String userId) throws Exception {
		String str = doHttpGet(UrlStr.FAVORITES, new BasicNameValuePair("id",
				userId));
		return Status.getStatusList(new JSONArray(str));
	}

	public String createFavorites(String statusId) throws Exception {
		return doHttpPost(UrlStr.FAVORITES_CREATE, new BasicNameValuePair("id",
				statusId));
	}

	public String deleteFavorites(String statusId) throws Exception {
		return doHttpPost(UrlStr.FAVORITES_DESTROY, new BasicNameValuePair(
				"id", statusId));
	}

	public String createFriendships(String userId) throws Exception {
		return doHttpPost(UrlStr.FRIENDSHIPS_CREATE, new BasicNameValuePair(
				"id", userId));
	}

	public String deleteFriendships(String userId) throws Exception {
		return doHttpPost(UrlStr.FRIENDSHIPS_DESTROY, new BasicNameValuePair(
				"id", userId));
	}

	public boolean isFriendships(String user_a, String user_b) throws Exception {
		String str = doHttpGet(UrlStr.FRIENDSHIPS_EXISTS,
				new BasicNameValuePair("user_a", user_a),
				new BasicNameValuePair("user_b", user_b));
		return "true".equals(str);
	}
	
	

}
